//
//  PARealNameAuthView.m
//  PAMobileWallet
//
//  Created by Pay_SMP002 on 13-7-3.
//  Copyright (c) 2013年 平安付. All rights reserved.
//

#import "PARealNameAuthView.h"
#import "PARealNameAuthViewController.h"
#import "UIColor+Extend.h"

@implementation PARealNameAuthView
@synthesize nameTF;
@synthesize cardTF;
@synthesize loginpassTF;
@synthesize loginpassLable;
@synthesize paypassTF;
@synthesize paypassLable;
@synthesize agreeLable;

@synthesize nameImage;
@synthesize cardImage;
@synthesize loginpassImage;
@synthesize paypassImage;
@synthesize lineImage;

@synthesize readButton;
@synthesize authButton;
@synthesize agreeButton;

@synthesize warringButton;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        CGSize size = frame.size;

        cellSize = CGSizeMake(578/2, 90/2);
        
        warringButton = [UIButton buttonWithType:UIButtonTypeCustom];
        warringButton.frame = CGRectMake(JULI_LOGINTF, 44+16, SIZE_LOGINTF.width, 80/2);
        [warringButton setBackgroundImage:[UIImage imageNamed:@"register_warning.png"] forState:UIControlStateNormal];
        warringButton.titleLabel.textColor = [UIColor colorWithHexString:@"414346"];
        warringButton.enabled = NO;
        warringButton.hidden = YES;
        [self addSubview:warringButton];
        
        // 线
        lineImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"register_line.png"]];
        lineImage.frame = CGRectMake(5, 44+16+cellSize.height*6+5*5 + 1, 310, 1);
        lineImage.backgroundColor = [UIColor clearColor];
        [self addSubview:lineImage];

        agreeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        agreeButton.frame = CGRectMake((size.width - cellSize.width)/2, 44+16+cellSize.height*6+5*7, 24, 24);
        [agreeButton addTarget:self action:@selector(changeState:) forControlEvents:UIControlEventTouchUpInside];
        [agreeButton setBackgroundImage:[UIImage imageNamed:@"register_seclectYES.png"] forState:UIControlStateNormal];
        agreeButton.selected = NO;
        [self addSubview:agreeButton];
        
        agreeLable = [[UILabel alloc] initWithFrame:CGRectMake((size.width - cellSize.width)/2+24+10, 44+16+cellSize.height*6+5*7, 140, 24)];
        agreeLable.textColor = [UIColor grayColor];
        agreeLable.text = @"同意协议";
        agreeLable.font = [UIFont systemFontOfSize:18.0];
        [self addSubview:agreeLable];
        
        readButton = [UIButton buttonWithType:UIButtonTypeCustom];
        readButton.frame = CGRectMake(size.width - (size.width - cellSize.width)/2-80, 44+16+cellSize.height*6+5*7, 80, 24);
        [readButton addTarget:self action:@selector(buttonActive:) forControlEvents:UIControlEventTouchUpInside];
        [readButton setTitle:@"阅读协议" forState:UIControlStateNormal];
        [readButton setTitleColor:[UIColor colorWithHexString:@"3999D0"] forState:UIControlStateNormal];
        [readButton setBackgroundColor:[UIColor clearColor]];
        readButton.titleLabel.font = [UIFont systemFontOfSize:18.0];
        readButton.tag = 10;
        [self addSubview:readButton];
        
        authButton = [UIButton buttonWithType:UIButtonTypeCustom];
        authButton.frame = CGRectMake((size.width - cellSize.width)/2, 44+16+cellSize.height*6+5*7 + 35, cellSize.width, cellSize.height);
        authButton.backgroundColor = [UIColor clearColor];
        [authButton setTitle:@"注册" forState:UIControlStateNormal];
        [authButton setBackgroundImage:[UIImage imageNamed:@"button_B1.png"] forState:UIControlStateDisabled];
        [authButton setTitleColor:ENABLE_COLOR forState:UIControlStateDisabled];
        authButton.titleLabel.font = [UIFont systemFontOfSize:18.0];
        [authButton addTarget:self action:@selector(buttonActive:) forControlEvents:UIControlEventTouchUpInside];
        authButton.enabled = NO;
        authButton.tag = 20;
        [self addSubview:authButton];
        
        //真是姓名
        nameImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"card_enter.png"]];
        nameImage.frame = CGRectMake((size.width - cellSize.width)/2, 44+16, cellSize.width, cellSize.height);
        nameImage.backgroundColor = [UIColor clearColor];
        [self addSubview:nameImage];
        
        nameTF = [[UITextField alloc] initWithFrame:CGRectMake((size.width - cellSize.width)/2+20, 44+16, cellSize.width-20, cellSize.height)];
        nameTF.keyboardType = UIKeyboardTypeNamePhonePad;
        nameTF.returnKeyType = UIReturnKeyNext;
        [nameTF setBorderStyle:UITextBorderStyleNone]; //外框类型
        nameTF.placeholder = @"真实姓名"; //默认显示的字
        nameTF.secureTextEntry = NO; //密码
        nameTF.contentHorizontalAlignment = NSTextAlignmentCenter;
        nameTF.contentVerticalAlignment = NSTextAlignmentLeft;
        nameTF.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        //[nameTF setBackground:[UIImage imageNamed:@"card_enter.png"]];
        [self addSubview:nameTF];
        
        //身份证号
        cardImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"card_enter.png"]];
        cardImage.frame = CGRectMake((size.width - cellSize.width)/2, 44+16+cellSize.height+5, cellSize.width, cellSize.height);
        cardImage.backgroundColor = [UIColor clearColor];
        [self addSubview:cardImage];

        cardTF = [[UITextField alloc] initWithFrame:CGRectMake((size.width - cellSize.width)/2+20, 44+16+cellSize.height+5, cellSize.width-20, cellSize.height)];
        cardTF.keyboardType = UIKeyboardTypeASCIICapable;
        cardTF.returnKeyType = UIReturnKeyNext;
        [cardTF setBorderStyle:UITextBorderStyleNone]; //外框类型
        cardTF.placeholder = @"身份证号码"; //默认显示的字
        cardTF.secureTextEntry = NO; //密码
        cardTF.contentHorizontalAlignment = NSTextAlignmentCenter;
        cardTF.contentVerticalAlignment = NSTextAlignmentLeft;
        cardTF.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        //[cardTF setBackground:[UIImage imageNamed:@"card_enter.png"]];
        [self addSubview:cardTF];

        //登录密码
        loginpassImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"card_enter.png"]];
        loginpassImage.frame = CGRectMake((size.width - cellSize.width)/2, 44+16+cellSize.height*2+5*2, cellSize.width, cellSize.height);
        loginpassImage.backgroundColor = [UIColor clearColor];
        [self addSubview:loginpassImage];

        loginpassTF = [[UITextField alloc] initWithFrame:CGRectMake((size.width - cellSize.width)/2+20, 44+16+cellSize.height*2+5*2, cellSize.width-20, cellSize.height)];
        loginpassTF.keyboardType = UIKeyboardTypeASCIICapable;
        loginpassTF.returnKeyType = UIReturnKeyNext;
        [loginpassTF setBorderStyle:UITextBorderStyleNone]; //外框类型
        loginpassTF.placeholder = @"登录密码"; //默认显示的字
        loginpassTF.secureTextEntry = NO; //密码
        loginpassTF.contentHorizontalAlignment = NSTextAlignmentCenter;
        loginpassTF.contentVerticalAlignment = NSTextAlignmentLeft;
        loginpassTF.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        //[loginpassTF setBackground:[UIImage imageNamed:@"card_enter.png"]];
        [self addSubview:loginpassTF];

        loginpassLable = [[UILabel alloc] initWithFrame:CGRectMake((size.width - cellSize.width)/2, 44+16+cellSize.height*3+5*3, cellSize.width, cellSize.height)];
        loginpassLable.textColor = [UIColor grayColor];
        loginpassLable.textAlignment = NSTextAlignmentLeft;
        loginpassLable.text = @"有6-20个英文字母,数字或字符组成";
        loginpassLable.font = [UIFont systemFontOfSize:15.0];
        [self addSubview:loginpassLable];

        //支付密码
        paypassImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"card_enter.png"]];
        paypassImage.frame = CGRectMake((size.width - cellSize.width)/2, 44+16+cellSize.height*4+5*4, cellSize.width, cellSize.height);
        paypassImage.backgroundColor = [UIColor clearColor];
        [self addSubview:paypassImage];

        paypassTF = [[UITextField alloc] initWithFrame:CGRectMake((size.width - cellSize.width)/2+20, 44+16+cellSize.height*4+5*4, cellSize.width-20, cellSize.height)];
        paypassTF.keyboardType = UIKeyboardTypeASCIICapable;
        paypassTF.returnKeyType = UIReturnKeyDone;
        [paypassTF setBorderStyle:UITextBorderStyleNone]; //外框类型
        paypassTF.placeholder = @"支付密码"; //默认显示的字
        paypassTF.secureTextEntry = NO; //密码
        paypassTF.contentHorizontalAlignment = NSTextAlignmentCenter;
        paypassTF.contentVerticalAlignment = NSTextAlignmentLeft;
        paypassTF.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        //[paypassTF setBackground:[UIImage imageNamed:@"card_enter.png"]];
        [self addSubview:paypassTF];

        paypassLable = [[UILabel alloc] initWithFrame:CGRectMake((size.width - cellSize.width)/2, 44+16+cellSize.height*5+5*5, cellSize.width, cellSize.height)];
        paypassLable.textColor = [UIColor grayColor];
        paypassLable.textAlignment = NSTextAlignmentLeft;
        paypassLable.text = @"有6-20个英文字母,数字或字符组成";
        paypassLable.font = [UIFont systemFontOfSize:15.0];
        [self addSubview:paypassLable];
    }
    
    return self;
}

- (IBAction)buttonActive:(id)sender
{
    if (self.parentController && [self.parentController respondsToSelector:@selector(sendButton:)]) {
        [self.parentController performSelector:@selector(sendButton:) withObject:sender];
    }
}

- (void)changeState:(UIButton *)sender
{
    UIButton * receiveButton = (UIButton *)sender;
    if (!receiveButton.selected)
    {
        [receiveButton setBackgroundImage:[UIImage imageNamed:@"register_seclectNO.png"] forState:UIControlStateNormal];
        receiveButton.selected = !receiveButton.selected;
        [authButton setBackgroundImage:[UIImage imageNamed:@"card_blank_1.png"] forState:UIControlStateDisabled];
        [authButton setTitleColor:ENABLE_COLOR forState:UIControlStateDisabled];
        authButton.enabled = NO;
    }
    else
    {
        [receiveButton setBackgroundImage:[UIImage imageNamed:@"register_seclectYES.png"] forState:UIControlStateNormal];
        receiveButton.selected = !receiveButton.selected;
        
        if ([nameTF.text length]>0 && [cardTF.text length]>0 && [loginpassTF.text length]>0 && [paypassTF.text length] && [paypassTF.text length]>0)
        {
            [authButton setBackgroundImage:[UIImage imageNamed:@"card_blank.png"] forState:UIControlStateNormal];
            authButton.enabled = YES;
        }
        else
        {
            [authButton setBackgroundImage:[UIImage imageNamed:@"card_blank_1.png"] forState:UIControlStateDisabled];
            [authButton setTitleColor:ENABLE_COLOR forState:UIControlStateDisabled];
            authButton.enabled = NO;
        }
    }
}

#pragma mark - UITextFieldDelegate  methods

@end
